package com.hamster.crmpre.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.config.LoginInterceptor;
import com.hamster.crmpre.mapper.EmployeeMapper;
import com.hamster.crmpre.mapper.LeveragePlanInformationMapper;
import com.hamster.crmpre.pojo.Employee;
import com.hamster.crmpre.pojo.Leverage;
import com.hamster.crmpre.pojo.LeveragePlanInformation;
import com.hamster.crmpre.pojo.dto.SaveLeveragePlanDTO;
import com.hamster.crmpre.service.EmployeeQueryService;
import com.hamster.crmpre.service.EmployeeService;
import com.hamster.crmpre.service.LeveragePlanService;
import com.hamster.crmpre.util.JsonUtils;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author ：Lizhipeng
 * @date ：Created in 2019/12/28 10:56
 * @description：杠杆计划控制器
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("leveragePlan")
public class LeveragePlanController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private LeveragePlanService leveragePlanService;
    @Autowired
    private EmployeeQueryService employeeQueryService;
    @Autowired
    private LeveragePlanInformationMapper leveragePlanInformationMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 根据id获得杠杆计划信息的数据请求
     * @param id 杠杆计划信息的唯一标识
     * @return 前台杠杆计划页面所需的信息
     */
//    @GetMapping("getLeveragePlan")
    public Map<String,Object> getMap(@RequestParam(value = "id") String id){
        Map<String, Object> map = new HashMap<>();
        //目标、本金查询填充
        Leverage leverage = leveragePlanService.getLeverage(id);
        map.put("本金", leverage.getPrincipal());
        map.put("目标", leverage.getTargetAmount());
        //待改
        List<Employee> listByDepartment = employeeQueryService.listByDepartment("服务部");
        map.put("服务部经理",listByDepartment);
        //员工信息查询填充
        List<Employee> employeesList =  employeeQueryService.list();
        map.put("员工信息", employeesList);

        return map;
    }

    @GetMapping("getLeveragePlan")
    public Map<String,Object> getMap(){
        Map<String, Object> map = new HashMap<>();
        //目标、本金查询填充
        List<Leverage> leverageList = leveragePlanService.list();
        map.put("leverage", leverageList);
        //查询服务部员工列表
        List<Employee> listByDepartment = employeeQueryService.listByDepartment("2");
        map.put("serviceManager",listByDepartment);
        //员工信息查询填充
        List<Employee> employeesList =  employeeQueryService.list();
        map.put("employee", employeesList);

        return map;
    }

    /**
     * 获取杠杆计划 select下拉框
     * @return
     */
    @GetMapping("leverageList")
    public List<Leverage> leverageList(){
        List<Leverage> leverageList = leveragePlanService.list();
        return leverageList;
    }

    /**
     * 查询所有员工select下拉框
     * @return
     */
    @GetMapping("getEmployeeList")
    public List<Employee> getEmployeeList(){
        List<Employee> employeesList =  employeeQueryService.list();
        return employeesList;
    }

    /**
     * 查询服务部员工 select 下拉框
     * @return
     */
    @GetMapping("getServiceManager")
    public List<Employee> getServiceManager(){
        //查询服务部员工列表
        List<Employee> listByDepartment = employeeQueryService.listByDepartment("2");
        return listByDepartment;
    }



    /**
     * 发起杠杆计划信息的请求
     * @param leveragePlanInformation
     * @return
     */
    @PostMapping("getLeveragePlan/determine")
    public ResponseEntity<Void> leveragePlanDetermine(@RequestBody LeveragePlanInformation leveragePlanInformation){
        leveragePlanService.insert(leveragePlanInformation);
        return ResponseEntity.ok().build();
    }


    /**
     * 提交保存杠杆计划，判断是否达标，并进行对应的操作
     * @param saveLeveragePlanDTO
     * @return
     */
    @PostMapping("saveLeveragePlan")
    public ResponseEntity<Void> saveLeveragePlan(@RequestBody SaveLeveragePlanDTO saveLeveragePlanDTO){
        leveragePlanService.saveLeveragePlan(saveLeveragePlanDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取员工select下拉框
     * @return
     */
    @GetMapping("listEmployee")
    public ResponseEntity<List<Employee>> listEmployee(){
        List<Employee> list = employeeService.listEmployee();
        return ResponseEntity.ok(list);
    }



    /**
     * 获取该登录着的部门的员工select下拉框
     * @return
     */
    @GetMapping("listDepartmentEmployee")
    public ResponseEntity<List<Employee>> listDepartmentEmployee(){
        List<Employee> list = employeeService.listDepartmentEmployee();
        return ResponseEntity.ok(list);
    }


    /**
     * 获取分栏的杠杆计划列表
     */
    @GetMapping("listLeveragePlanInformation")
    public ResponseEntity<List<LeveragePlanInformation>>  listLeveragePlanInformation(@RequestParam(required = false) String name){
        String userId = LoginInterceptor.getLoginUser().getUserId();
        QueryWrapper<LeveragePlanInformation> leveragePlanInformationQueryWrapper = new QueryWrapper<>();
        leveragePlanInformationQueryWrapper.eq("state","0").eq("verify","1").eq("employee_id",userId);
        if (StringUtils.isNotBlank(name)){
            leveragePlanInformationQueryWrapper.like("customer_name","%"+name+"%");
        }
        leveragePlanInformationQueryWrapper.orderByDesc("create_time");

        List<LeveragePlanInformation> list = leveragePlanInformationMapper.selectList(leveragePlanInformationQueryWrapper);

        //把各个负责人的姓名查出来
        for (LeveragePlanInformation leveragePlanInformation : list) {
            ArrayList<String> name1 = new ArrayList<>();
            for (String s : JsonUtils.parseList(leveragePlanInformation.getOriginalManager(), String.class)) {

                name1.add(employeeMapper.queryNameById(s));
            }
            leveragePlanInformation.setOriginalManager(JsonUtils.serialize(name1));

            ArrayList<String> name2 = new ArrayList<>();
            for (String s : JsonUtils.parseList(leveragePlanInformation.getWxGroupResponsible(), String.class)) {
                name2.add(employeeMapper.queryNameById(s));
            }
            leveragePlanInformation.setWxGroupResponsible(JsonUtils.serialize(name2));

            ArrayList<String> name3 = new ArrayList<>();
            for (String s : JsonUtils.parseList(leveragePlanInformation.getWxGroupParticipate(), String.class)) {
                name3.add(employeeMapper.queryNameById(s));
            }
            leveragePlanInformation.setWxGroupParticipate(JsonUtils.serialize(name3));

            ArrayList<String> name4 = new ArrayList<>();
            for (String s : JsonUtils.parseList(leveragePlanInformation.getPerformGroupResponsible(), String.class)) {
                name4.add(employeeMapper.queryNameById(s));
            }
            leveragePlanInformation.setPerformGroupResponsible(JsonUtils.serialize(name4));

            ArrayList<String> name5 = new ArrayList<>();
            for (String s : JsonUtils.parseList(leveragePlanInformation.getPerformGroupLecturer(), String.class)) {
                name5.add(employeeMapper.queryNameById(s));
            }
            leveragePlanInformation.setPerformGroupLecturer(JsonUtils.serialize(name5));


            ArrayList<String> name6 = new ArrayList<>();
            for (String s : JsonUtils.parseList(leveragePlanInformation.getPerformGroupParticipate(), String.class)) {
                name6.add(employeeMapper.queryNameById(s));
            }
            leveragePlanInformation.setPerformGroupParticipate(JsonUtils.serialize(name6));

            ArrayList<String> name7 = new ArrayList<>();
            for (String s : JsonUtils.parseList(leveragePlanInformation.getSecurityGroupParticipate(), String.class)) {
                name7.add(employeeMapper.queryNameById(s));
            }
            leveragePlanInformation.setSecurityGroupParticipate(JsonUtils.serialize(name7));

        }



        return ResponseEntity.ok(list);
    }




}
